package cn.airrunner.voicerecorder;

import android.app.Service;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.ITelephony;
import com.android.internal.telephony.ITelephony.Stub;
import java.io.File;
import java.util.Date;
import java.util.Timer;

public class CallService extends Service
{
  public static final String LOG_TAG = "CallService";
  public static final int VOICE_CALL = 4;
  public static final int VOICE_DOWNLINK = 3;
  public static final int VOICE_UPLINK = 2;
  public static String file_format;
  public static int max_file_counts = 10;
  public static int mode = 3;
  public static int playback_delay;
  public static boolean usePlayback;
  public static boolean usingAPI;
  private VoiceRecorderApp app;
  PhoneStateListener listener;
  public Date m_last_ended_time;
  public Date m_last_started_time;
  public int m_last_state;
  public String m_linenumber;
  MediaRecorder recorder;

  static
  {
    boolean bool = usingAPI;
    playback_delay = usePlayback;
    file_format = ".wav";
  }

  public CallService()
  {
    MediaRecorder localMediaRecorder = new MediaRecorder();
    this.recorder = localMediaRecorder;
    this.app = null;
    CallService.1 local1 = new CallService.1(this);
    this.listener = local1;
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    SharedPreferences localSharedPreferences = getSharedPreferences("cn.airrunner.voicerecorder_preferences", 0);
    max_file_counts = Integer.parseInt(localSharedPreferences.getString("list_counts", "10"));
    mode = Integer.parseInt(localSharedPreferences.getString("record_type", "3"));
    boolean bool1 = localSharedPreferences.getBoolean("playback_enable", null);
    boolean bool2 = usePlayback;
    playback_delay = Integer.parseInt(localSharedPreferences.getString("playback_delay", "0"));
    file_format = localSharedPreferences.getString("file_format", ".wav");
    TelephonyManager localTelephonyManager = (TelephonyManager)getSystemService("phone");
    PhoneStateListener localPhoneStateListener = this.listener;
    localTelephonyManager.listen(localPhoneStateListener, 32);
    VoiceRecorderApp localVoiceRecorderApp = (VoiceRecorderApp)getApplication();
    this.app = localVoiceRecorderApp;
    this.app.setCallService(this);
    super.onCreate();
  }

  public void onStart(Intent paramIntent, int paramInt)
  {
    String str1 = "android.intent.extra.PHONE_NUMBER";
    if (paramIntent == null);
    while (true)
    {
      return;
      if ((paramIntent.getExtras() != null) && (paramIntent.getExtras().containsKey(str1)))
      {
        String str2 = paramIntent.getExtras().getString(str1);
        this.m_linenumber = str2;
      }
      super.onStart(paramIntent, paramInt);
    }
  }

  public void startPlayback()
  {
    if (this.m_last_state == 1);
    try
    {
      ITelephony.Stub.asInterface(ServiceManager.getService("phone")).answerRingingCall();
      String str = String.valueOf(VoiceRecorderApp.dataPath);
      NativeTask.startPlayback(str + "/files/" + "myvoice.pcm", 2);
      return;
    }
    catch (RemoteException localRemoteException)
    {
      while (true)
        localRemoteException.printStackTrace();
    }
  }

  public void updateContent(String paramString1, String paramString2, Date paramDate1, Date paramDate2)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("linenumber", paramString2);
    localContentValues.put("filepath", paramString1);
    Long localLong1 = Long.valueOf(paramDate1.getTime());
    localContentValues.put("started_time", localLong1);
    Long localLong2 = Long.valueOf(paramDate2.getTime());
    localContentValues.put("ended_time", localLong2);
    ContentResolver localContentResolver1 = getContentResolver();
    Uri localUri1 = VoiceRecorder.Recorder.CONTENT_URI;
    Uri localUri2 = localContentResolver1.insert(localUri1, localContentValues);
    ContentResolver localContentResolver2 = getContentResolver();
    Uri localUri3 = VoiceRecorder.Recorder.CONTENT_URI;
    String[] arrayOfString = VoiceRecorder.Recorder.PROJECTION;
    Cursor localCursor = localContentResolver2.query(localUri3, arrayOfString, null, null, "_ID ASC");
    localCursor.moveToFirst();
    int i = localCursor.getCount();
    int j = max_file_counts;
    if (i >= j)
    {
      int k = localCursor.getColumnIndex("_id");
      int m = localCursor.getInt(k);
      int n = localCursor.getColumnIndex("filepath");
      String str = localCursor.getString(n);
      Uri localUri4 = VoiceRecorder.Recorder.CONTENT_URI;
      long l = m;
      localUri2 = ContentUris.withAppendedId(localUri4, l);
      File localFile = new File(str);
      if (localFile.exists())
        localFile.delete();
      getContentResolver().delete(localUri2, null, null);
    }
    localCursor.close();
  }

  public void updateDB()
  {
    NativeTask.stop();
    this.app.releaseWakeLock();
    Date localDate1 = this.m_last_ended_time;
    Date localDate2 = this.m_last_started_time;
    String str1 = this.m_linenumber;
    String str2 = NativeTask.file_path;
    File[] arrayOfFile = new File(str2).listFiles();
    long l1 = 0L;
    String str3 = null;
    String str4 = null;
    Object localObject1 = null;
    int i = arrayOfFile.length;
    if (localObject1 >= i)
      return;
    if ((!arrayOfFile[localObject1].isFile()) || (!arrayOfFile[localObject1].getName().contains(".pcm")) || (arrayOfFile[localObject1].getName().equals("voice.pcm")));
    Object localObject2;
    do
    {
      long l3;
      do
      {
        localObject1++;
        break;
        long l2 = arrayOfFile[localObject1].lastModified();
        l3 = localDate1.getTime() + 3000L;
      }
      while (l1 > l3);
      long l4 = localDate2.getTime();
    }
    while (l1 < localObject2);
    str3 = arrayOfFile[localObject1].getName();
    if (file_format.equals(".amr"))
    {
      str4 = arrayOfFile[localObject1].getName().replace(".pcm", ".amr");
      String str5 = String.valueOf(NativeTask.file_path);
      String str6 = str5 + "/" + str3;
      String str7 = String.valueOf(NativeTask.file_path);
      String str8 = str7 + "/" + str4;
      AMRUtil.EncodeAMRFile(str6, str8);
    }
    while (true)
    {
      arrayOfFile[localObject1].delete();
      updateContent(str4, str1, localDate2, localDate1);
      break;
      str4 = arrayOfFile[localObject1].getName().replace(".pcm", ".wav");
      String str9 = String.valueOf(NativeTask.file_path);
      String str10 = str9 + "/" + str3;
      String str11 = String.valueOf(NativeTask.file_path);
      String str12 = str11 + "/" + str4;
      AMRUtil.PCM2Wave(str10, str12);
    }
  }

  public void updateState(int paramInt, String paramString)
  {
    switch (paramInt)
    {
    default:
    case 1:
    case 0:
    case 2:
    }
    while (true)
    {
      return;
      this.m_linenumber = paramString;
      this.m_last_state = paramInt;
      usePlayback = i;
      if (i == 0)
        continue;
      if (playback_delay == 0)
      {
        CallService.2 local2 = new CallService.2(this);
        new Timer().schedule(local2, 1000L);
        continue;
      }
      CallService.3 local3 = new CallService.3(this);
      Timer localTimer = new Timer();
      long l1 = playback_delay * 1000;
      localTimer.schedule(local3, l1);
      continue;
      int i = this.m_last_state;
      if (i == 2)
      {
        this.m_last_state = paramInt;
        localDate1 = new Date();
        this.m_last_ended_time = localDate1;
        localDate1 = this.m_last_ended_time;
        long l2 = System.currentTimeMillis();
        Object localObject1;
        localDate1.setTime(localObject1);
        usePlayback = localDate1;
        if (localDate1 != null)
          NativeTask.stopPlayback();
        usingAPI = localDate1;
        if (localDate1 != null)
        {
          this.recorder.stop();
          this.recorder.reset();
          String str1 = String.valueOf(this.m_last_started_time.getTime());
          String str2 = str1 + ".amr";
          String str3 = this.m_linenumber;
          Date localDate2 = this.m_last_started_time;
          Date localDate3 = this.m_last_ended_time;
          updateContent(str2, str3, localDate2, localDate3);
          this.app.releaseWakeLock();
          this.m_linenumber = null;
          continue;
        }
        CallService.4 local4 = new CallService.4(this);
        new Timer().schedule(local4, 3000L);
        continue;
      }
      this.m_last_state = paramInt;
      continue;
      Date localDate1 = new Date();
      this.m_last_started_time = localDate1;
      localDate1 = this.m_last_started_time;
      long l3 = System.currentTimeMillis();
      Object localObject2;
      localDate1.setTime(localObject2);
      this.m_last_state = paramInt;
      usingAPI = localDate1;
      if (localDate1 != null)
        continue;
      NativeTask.started = localDate1;
      if (localDate1 != null)
        continue;
      this.app.aquireWakeLock();
      NativeTask.start(mode);
    }
  }
}
